#include "Owner.h"

// KONSTRUKTOR - DESTRUKTOR
ownerList		CreateEmptyOwnerList()
{
	//kamus lokal
	ownerList P;
	//algoritma
	P=NULL;
	return P;
}
ownerAddress	AllocateOwnerList(nodeAddress A)
{
	//kamus lokal
	//algoritma
	AllocateOwnerListAdv(A,NULL);
}
ownerAddress	AllocateOwnerListAdv(nodeAddress A, ownerAddress next)
{
	//kamus lokal
	ownerAddress O;
	//algoritma
	O = (ownerAddress)malloc(sizeof(owner));
	if (O!=NULL) {
		NODE(O)= A;
		NEXT(O)= next;
	}
	return O;
}
void			DeallocateOwner(owner *e)
{
	//kamus lokal
	//algoritma
	free(e);
}

// OPERASI
void			AddOwner(ownerList *EL, ownerAddress e)
/**********************************************************************************
	Menambahkan e ke EL
	I.S. : 	e sudah dialokasi
	F.S. : 	EL berisi e
**********************************************************************************/ 
{
	//kamus lokal
	//algoritma
	NEXT(e)= FIRST(*EL);
	FIRST(*EL)= e;
}

ownerAddress	SearchOwnerByNode(ownerList *EL, nodeAddress e) {
	if(ISLINKEDLISTEMPTY(*EL)) return NULL;
	else {
		ownerAddress retval;
		TRAVERSETOEND(retval,FIRST(*EL))
			if(NODE(retval)==e) break;
		
		if(NODE(retval)==e) return retval;
		else return NULL;
	}
}

void PrintListOwner(ownerList O)
{
	//kamus lokal
	ownerAddress e;
	//algoritma
	if(ISLINKEDLISTEMPTY(O)){
		puts("-EMPTY-");
	}else{
		e=FIRST(O);
		while(NEXT(e)!=NULL){
			printf("%s",NODENAME(NODE(e)).s); puts("");
			e = NEXT(e);
		}printf("%s", NODENAME(NODE(e)).s); puts("");
	}
}

void		DestroyOwner(ownerList *O, ownerAddress A) {
	if(FIRST(*O)==A) {
		FIRST(*O)=NEXT(A);
	}
	else {
		ownerAddress M;
		TRAVERSETOEND(M,FIRST(*O))
			if(NEXT(M) == A) break;
			
		NEXT(M) = NEXT(A);
	}
	
	DeallocateOwner(A);
}

void		DestroyOwnerList(ownerList *O) {
	ownerAddress M;
	ownerAddress next;
	TRAVERSETOEND3(M,next,FIRST(*O)) {
		DestroyOwner(O,M);
	}
}

ownerList CopyOwnerList(ownerList O){
	ownerList Out=CreateEmptyOwnerList();
	ownerAddress OA=FIRST(O);
	if (OA==NULL){
		return Out;
	}
	else {
		while (OA!=NULL){
            AddOwner(&Out,AllocateOwnerList(NODE(OA)));   
			OA=NEXT(OA);
        }
        return Out;
	}
}
